#include "headfile.h"


void IIC_Init(void)
{
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
	
	GPIO_InitTypeDef GPIO_struct;
	GPIO_struct.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_struct.GPIO_OType = GPIO_OType_OD;
	GPIO_struct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
	GPIO_struct.GPIO_Speed = GPIO_Fast_Speed;
	
	GPIO_Init(GPIOB,&GPIO_struct);
}

void setModeOut(void)
{
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
	
	GPIO_InitTypeDef GPIO_struct;
	GPIO_struct.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_struct.GPIO_OType = GPIO_OType_OD;
	GPIO_struct.GPIO_Pin = GPIO_Pin_7;
	GPIO_struct.GPIO_Speed = GPIO_Fast_Speed;
	
	GPIO_Init(GPIOB,&GPIO_struct);
	
}

void setModeIn(void)
{
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
	
	GPIO_InitTypeDef GPIO_struct;
	GPIO_struct.GPIO_Mode = GPIO_Mode_IN;
	GPIO_struct.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_struct.GPIO_Pin = GPIO_Pin_7;
	GPIO_struct.GPIO_Speed = GPIO_Fast_Speed;
	
	GPIO_Init(GPIOB,&GPIO_struct);
	
}

void stop_signal(void)
{
	setModeOut();
	SCL_WRITE(0);
	SDA_WRITE(0);
	delay_us(2);
	SCL_WRITE(1);
	delay_us(5);
	SDA_WRITE(1);
}

void start_signal(void)
{
	setModeOut();
	SCL_WRITE(1);
	SDA_WRITE(1);
	delay_us(5);
	SDA_WRITE(0);
	delay_us(5);
	SCL_WRITE(0);
}

unsigned char get_ask(void)
{
	SCL_WRITE(0);
	setModeIn();
	SCL_WRITE(1);
	delay_us(5);
	
	if(RESET == GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7))
	{
		SCL_WRITE(0);
		delay_us(5);
		return 0;
	}

	return 1;
}

void send_ask(bool n)
{
	SCL_WRITE(0);
	setModeOut();
	SCL_WRITE(0);
	delay_us(5);
	if(0 == n)
	{
		SDA_WRITE(0);
	}
	else
	{
		SDA_WRITE(1);
	}
	SCL_WRITE(1);
	delay_us(5);
	SCL_WRITE(0);
	delay_us(5);
}

void IIC_Send_Data(unsigned char data)
{
	SCL_WRITE(0);
	setModeOut();
	SCL_WRITE(0);
	delay_us(2);
	for(int i = 7; i >= 0; i--)
	{
		if(data & (1 << i))
			SDA_WRITE(1);
		else
			SDA_WRITE(0);
		delay_us(2);
		SCL_WRITE(1);
		delay_us(2);
		SCL_WRITE(0);
		delay_us(2);
	}
	
}

unsigned char IIC_Recv_Data(void)
{
	SCL_WRITE(0);
	setModeIn();
	SCL_WRITE(0);
	delay_us(2);
	unsigned char data = 0;
	for(int i = 7; i >= 0; i--)
	{
		SCL_WRITE(1);
		delay_us(2);
		if(SET == GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7))
			data |= 1 << i;
		SCL_WRITE(0);
		delay_us(2);
	}
	
	return data;
}

void IIC_EEPROM_Write_Data(unsigned char addr,unsigned char data)
{
	start_signal();
	IIC_Send_Data(0xA0);
	if(1 == get_ask())
	{
		printf("DEVICE ADDRESS ack fail!");
		stop_signal();
		return ;
	}
	IIC_Send_Data(addr);
	if(1 == get_ask())
	{
		printf("WORD ADDRESS ack fail!");
		stop_signal();
		return ;
	}
	IIC_Send_Data(data);
	if(1 == get_ask())
	{
		printf("DATA ack fail!");
		stop_signal();
		return ;
	}
	stop_signal();
}

unsigned char IIC_EEPROM_Read_Data(unsigned char addr)
{
	unsigned char data;
	start_signal();
	IIC_Send_Data(0xA0);
	if(1 == get_ask())
	{
		printf("DEVICE ADDRESS WRITE ack fail!");
		stop_signal();
	}
	else 
	{
		IIC_Send_Data(addr);
		if(1 == get_ask())
		{
			printf("WORD ADDRESS WRITE ack fail!");
			stop_signal();
		}
		else
		{
			start_signal();
			IIC_Send_Data(0xA1);
			if(1 == get_ask())
			{
				printf("DEVICE ADDRESS READ ack fail!");
				stop_signal();
			}
			else
			{
				data = IIC_Recv_Data();
				send_ask(1);
				stop_signal();
				return data;
			}
		}
	}
}	
